package toubiao.service.impl.init;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import toubiao.dao.support.RoleDaoI;
import toubiao.dao.support.UserDaoI;
import toubiao.domain.po.manage.PoRole;
import toubiao.domain.po.manage.PoUser;
import toubiao.service.init.InitUserServiceI;

@Service
public class InitUserServiceImpl implements InitUserServiceI{

	@Autowired
	private RoleDaoI roleDao;
	
	@Autowired
	private UserDaoI userDao;
	
	@Override
	public void clearAndInit(){
		clearUser();
		init();
		
	}
	
	@Override
	public void clearUser(){
		String hql="from PoUser";
		List<PoUser> userList=userDao.find(hql);
		for(PoUser user:userList){
			userDao.delete(user);
		}
	}
	
	@Override
	public void init(){
		initAdminUser();
	}
	
	    /* (非 Javadoc)
	    * 
	    * 
	    * @see toubiao.service.init.InitUserServiceI#initAdminUser()
	    */
	@Override
	public void initAdminUser() {
		PoUser user=new PoUser();
		user.setName("admin");
		user.setPwd("1");
		
		String hql="from PoRole r where r.name='admin'";
		List<PoRole> roleList=roleDao.find(hql);
		Set<PoRole> roleSet=new HashSet<>();
		roleSet.addAll(roleList);
		user.setRoleSet(roleSet);
		userDao.save(user);
	}
	
}
